import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class AnimationControl 
{
	private InternalAnimations model;
	private AnimationsPanel view;
	private Controller control;
	private Timer sort;
	private JPanel backPanel;
	private JSlider animationSpeed;
	
	public AnimationControl(Controller myControl)
	{
		control=myControl;
		model=new InternalAnimations(control, this, "bubbleSort()");
		view=new AnimationsPanel(this);
		final AnimationControl me=this;
		sort=new Timer(10, model.sort());
		
		
		
		
		
		backPanel=new JPanel();
		JPanel options=new JPanel();
		backPanel.setLayout(new BoxLayout(backPanel, BoxLayout.Y_AXIS));
		options.setLayout(new BoxLayout(options, BoxLayout.Y_AXIS));
		animationSpeed = new JSlider(JSlider.HORIZONTAL, 1, 1000, 10);
		animationSpeed.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				// TODO Auto-generated method stub
				JSlider source=(JSlider)e.getSource();
				//if(!source.getValueIsAdjusting())
				{
					sort.setDelay(source.getValue());
				}
			}
			
		});
		
		//JComboBox beginDrop = new JComboBox(control.accessNoUpdate("Sort Algorithms"));
		String[] sorting = new String[]{"bubbleSort()", "selectionSort()", "insertionSort()", "quickSort()"};
		JComboBox beginDrop=new JComboBox(sorting);
		beginDrop.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e) {
				model=new InternalAnimations(control, me, (String)(((JComboBox)e.getSource()).getSelectedItem()));
				sort=new Timer(animationSpeed.getValue(), model.sort());
				
			}
			
		});
		animationSpeed.setVisible(true);
		options.add(beginDrop);
		options.add(animationSpeed);
		
		JPanel fin=new JPanel();
		JButton go=new JButton("Go!");
		fin.add(go);
		go.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				sort.start();
				
			}
			
		});
		
		
		backPanel.add(options);
		backPanel.add(view);
		backPanel.add(fin);
		
		
		//sort.start();
		
	}
	
	public JPanel getBackPanel()
	{
		return backPanel;
	}
	
	public void standAlone()
	{
		JFrame frame=new JFrame();
		frame.add(backPanel);
		frame.setVisible(true);
		frame.pack();
	}
	
	public Color[] getRainbow()
	{
		Color[] rainbow=new Color[100];
		int r=255;
		int g=0;
		int b=0;
		int stage=1;
		int interval=10;
		rainbow[0]=new Color(r,g,b);
		for (int i=1;i<100;i++)
		{
			if (stage==1)
			{
				if (g>=(255-interval))
				{
					stage=2;
				}
				else
				{
					g+=interval;
					rainbow[i]=new Color(r,g,b);
				}
				
			}
			if (stage==2)
			{
				if (r<=(0+interval))
				{
					stage=3;
				}
				else
				{
					r-=interval;
					rainbow[i]=new Color(r,g,b);
				}
			}
			if (stage==3)
			{
				if (g<=(0+interval) || b>=(255-interval))
				{
					stage=4;
				}
				else
				{
					g-=interval;
					b+=interval;
					rainbow[i]=new Color(r,g,b);
				}
			}
			if (stage==4)
			{
				if (r>=(255-interval))
				{
					stage=5;
				}
				else
				{
					r+=interval;
					rainbow[i]=new Color(r,g,b);
				}
			}
		}
		return rainbow;
	}

	public void update() 
	{
		view.repaint();
	}

	public Color[] getColors() 
	{
		Color[] toReturn=new Color[100];
		Color[] rainbow=getRainbow();
		int[] ints=model.getInts();
		ArrayList<Integer> check=new ArrayList<Integer>(100);
		int placekeeper=0;
		for (int i=0;i<ints.length;i++)
		{
			if (!(placekeeper==100 || ints[i]>=100))
			{
				toReturn[placekeeper]=rainbow[ints[i]];
				placekeeper++;
			}
		}
		return toReturn;
	}
}
